package president;

import home.HomePage;
import includes.Sessions;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;


import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.Model;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.BKPayroll;
import controllers.Logs;

public class ViewPayroll extends WebPage{
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	
	private Model dateModel;
	private Model activityModel;
	private Model purposeModel;
	private Model disbursedByModel;
	private Model approvedByModel;
	private Model dateCreatedModel;
	private Model uploadedByModel;
	
	public ViewPayroll(int id) throws IOException, SQLException
	{
		add(new BookmarkablePageLink("home", PresidentHome.class));
		add(new BookmarkablePageLink("logoutPage", HomePage.class));
		
		final int formid = id;
		BKPayroll bk = (BKPayroll)sqlMap.queryForObject("BKPayroll.getById", id);
	
		dateModel = new Model(bk.getDate());
		activityModel = new Model(bk.getActivity());
		purposeModel = new Model(bk.getPurpose());
		disbursedByModel = new Model(bk.getDisbursedBy());
		approvedByModel = new Model(bk.getApprovedBy());
		dateCreatedModel = new Model(bk.getDate_created());
		uploadedByModel = new Model(bk.getUploader());
		
		Form form = new Form("viewForm");
		
		final String uploadby = ((Sessions)getSession()).getDesc();
		
		Date today = new Date();
		Timestamp timestamp = new Timestamp(today.getTime());
		final String ts = timestamp.toString();	
		
		form.add(new Button("approve"){
			private static final long serialVersionUID = 1L;
			
			public void onSubmit(){
			  try{
								
				BKPayroll bk = new BKPayroll();
				bk.setBkp_id(formid);
				bk.setStatus("APPROVED");
				sqlMap.update("BKPayroll.updateStatus", bk);
				
				Logs log = new Logs();
				log.setLog_activity("Approved Payroll Form: " + formid);
				log.setLog_officer(uploadby);
				log.setLog_time(ts);
				sqlMap.insert("Logs.insert", log);
				
				Payroll page = new Payroll();
				setResponsePage(page);
				
			  }catch(SQLException e){
				  e.printStackTrace();
			  }catch(IOException e){
				  e.printStackTrace();
			  }
			}
		});
		
		form.add(new Button("reject"){
			private static final long serialVersionUID = 1L;
			
			public void onSubmit(){
			  try{
								
				BKPayroll bk = new BKPayroll();
				bk.setBkp_id(formid);
				bk.setStatus("REJECTED");
				sqlMap.update("BKPayroll.updateStatus", bk);
				
				Logs log = new Logs();
				log.setLog_activity("Rejected Payroll Form: " + formid);
				log.setLog_officer(uploadby);
				log.setLog_time(ts);
				sqlMap.insert("Logs.insert", log);
				
				Payroll page = new Payroll();
				setResponsePage(page);
			  }catch(SQLException e){
				  e.printStackTrace();
			  }catch(IOException e){
				  e.printStackTrace();
			  }
			}
		});
		
		
		TextField date = new TextField("date", dateModel);
		form.add(date);
		
		TextField activity = new TextField("activity", activityModel);
		form.add(activity);
		
		TextField purpose = new TextField("purpose", purposeModel);
		form.add(purpose);
		
		TextField disbursedBy = new TextField("disbursedBy", disbursedByModel);
		form.add(disbursedBy);
		
		TextField approvedBy = new TextField("approvedBy", approvedByModel);
		form.add(approvedBy);
		
		TextField dateCreated = new TextField("dateCreated", dateCreatedModel);
		form.add(dateCreated);
		
		TextField uploader = new TextField("uploader", uploadedByModel);
		form.add(uploader);
		
		add(form);
	}

}
